/*  author:WangTong
*
*/
require("../index.scss")
var PaginationX = require("COMMON/modules/pagination-x/v1.0");
var loading = require('COMMON/js/util.loading.pc.js');
var ParseTemplate = ZFT.Util.ParseTemplate;
var Template = {
    tr: ParseTemplate(require("../tpl/tg.xtpl"))
}
var Main = ZFT.Util.Class({
    PAGE_SIZE: 15,
    dom: {
        filter: '#filter',
        beginDate: '#beginDate',
        endDate: '#endDate',
        state: '#state',
        tenant: '#tenant',
        mobile: '#mobile',
        btnQuery: '#btnQuery',
        tbBill: '#tbBill',
        pagination: $(".paginationWrap"),
    },
    xhrGetBillList: null,

    init: function () {
        var _this = this;
        var dom = _this.dom;

        this.params = {
            begin_month: '',
            end_month: '',
            name: '',
            mobile: '',
            status: '0',
            page: 1,
            page_size: this.PAGE_SIZE

        };

        this.ajaxGetBillList(this.params);
        $("#beginDate").datepicker({
            format: "yyyymm",
            startView: 1,
            minViewMode: 1,
            maxViewMode: 2,
            language: "zh-CN",
            autoclose: true
        });
        $('#endDate').datepicker({
            format: "yyyymm",
            startView: 1,
            minViewMode: 1,
            maxViewMode: 2,
            language: "zh-CN",
            autoclose: true
        });

        $(this.dom.btnQuery).on('click', function () {
            var beginDate = $.trim($(_this.dom.beginDate).val()),
                endDate = $.trim($(_this.dom.endDate).val());

            if (beginDate > endDate) {
                ZFT.Util.STip('fail', '起始时间不能大于结束时间');
                return false;
            }

            if ($(_this.dom.state).val() == 1) {
                $(_this.dom.btnPay).hide();
            } else {
                $(_this.dom.btnPay).show();
            }


            _this.params = {
                begin_month: beginDate,
                end_month: endDate,
                status: $(_this.dom.state).val(),
                name: $(_this.dom.tenant).val(),
                mobile: $(_this.dom.mobile).val(),
                page: 1,
                page_size: _this.PAGE_SIZE
            };

            //   console.log(_this.params)
            _this.selectedContract = [];
            _this.ajaxGetBillList(_this.params);
        });
    },



    ajaxGetBillList: function (params) {
        var _this = this;
        var tbody = $('#tbody');
        this.xhrGetBillList = this.ajaxRenderData({
            container: '#tbody',
            url: '/r/BillManager_trustIncomeBill',
            params: params,
            loading: {
                id: 'billLoading',
                text: '正在努力加载您的账单...',
                className: 'tb-bill-loading',
                colspan: 12,
                tag: 'td'
            },
            success: function (res) {
                _this.renderBillList(res.data.list);

                if (_this.pagination) {
                    _this.pagination.render({ current: res.data.current_page, total: res.data.total });
                } else {
                    _this.pagination = new PaginationX({
                        container: $(".paginationWrap"),
                        count: 7,
                        showTotal: true,
                        jump: true,
                        filter: true
                    });
                    _this.pagination.on("page.switch", function (toPage, current, total) {
                        var page_size = $(".paginationWrap").attr("data-size");
                        _this.params.page = toPage;
                        _this.params.page_size = page_size ? page_size : 15;
                        _this.ajaxGetBillList(_this.params);
                    });

                    _this.pagination.render({ current: res.data.current_page, total: res.data.total });
                }
            },
            error: function (res) {
                tbody.html('<tr><td colspan="12" align="center" class="tb-nodata">' + res.msg + '</td></tr>')

            },
            serverError: function () {
                tbody.html('<tr><td colspan="12" align="center" class="tb-nodata">' + res.msg + '</td></tr>')

            }
        })
    },

    ajaxRenderData: function (opts) {
        return ZFT.Util.Ajax(
            opts.url, {
                type: "POST",
                dataType: "json",
                params: opts.params,
                loading: function () {
                    var loadingOpts = {};
                    loadingOpts.id = opts.loading.id;
                    loadingOpts.text = opts.loading.text || '';
                    loadingOpts.className = opts.loading.className;
                    loadingOpts.tag = opts.loading.tag || '';
                    if (opts.loading.tag == 'td') {
                        loadingOpts.colspan = opts.loading.colspan ? opts.loading.colspan : '';
                    }

                    var html = loading(opts.loading.text, loadingOpts);
                    $(opts.container).html(html);
                },
                success: function (res) {
                    $('#' + opts.loading.id).remove();

                    if (res.code == 200 || res.code == 'success') {
                        opts.success && opts.success(res);
                    } else {
                        opts.error && opts.error(res);
                    }
                },
                serverError: function (xhr, txt) {

                }
            })
    },
    renderBillList: function (data) {
        var html = Template.tr({ data: data }),
            tbody = $('#tbody');
        tbody.html(html);
    },

});
$(function () {
    new Main();
})